package org.zhangxiao.paladin2.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.zhangxiao.paladin2.admin.cfg.ISysCfgService;
import org.zhangxiao.paladin2.admin.cfg.SystemCfg;
import org.zhangxiao.paladin2.common.exception.BizException;
import org.zhangxiao.paladin2.common.util.DTOUtils;

import java.util.Optional;

/**
 * 暂时还找不到方式来对此进行扩展
 */
@RestController("paladin2AdminSystemCfgCtrl")
@ConditionalOnMissingBean(type = "paladin2AdminSystemCfgCtrl")
public class SystemCfgCtrlDefault implements SystemCfgCtrl<SystemCfg> {
    @Autowired
    private ISysCfgService sysCfgService;

    @Override
    @GetMapping("/sys/cfg/system/get")
    public SystemCfg getCfg() {
        return Optional.ofNullable(sysCfgService.getCfg(SystemCfg.class))
                .orElse(new SystemCfg());
    }

    @Override
    @PostMapping("sys/cfg/system/save")
    public void saveCfg(@RequestBody @Validated SystemCfg cfg, BindingResult bindingResult) throws BizException {
        DTOUtils.checkThrow(bindingResult);
        sysCfgService.saveCfg(cfg);
    }

    @Override
    @GetMapping("sys/cfg/system/flush")
    public void flush() {
        sysCfgService.flushCfg(SystemCfg.class);
    }
}
